package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.SUCCESSFAIL;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo;
import cn.com.dhcc.turbo.entity.registry.wapper.SynchronizeInfoRowWapper;

/**
 * 信息同步
 * 
 * @日期：2014-6-6 上午9:27:28
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class SynchronizeInfoDao extends BaseDao {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);

	public PagerData<SynchronizeInfo> list(Pager pager, WhereCondition condition)
			throws DaoException {
		String sql = "select t1.*, t2.node_name fromnode_name,t3.node_name tonode_name,t4.node_name srcnode_name"
				+ " from "
				+ SI_TABLE("turbo_sync_info")
				+ "  t1 "
				+ " left join "
				+ SI_TABLE("turbo_node_info")
				+ "  t2 "
				+ "   on t1.fromnode = t2.id "
				+ " left join "
				+ SI_TABLE("turbo_node_info")
				+ "  t3"
				+ " 	on t1.tonode = t3.id"
				+ " left join "
				+ SI_TABLE("turbo_node_info")
				+ "  t4"
				+ " 	on t1.srcnode = t4.id";

		try {
			return CM.getDao().getPagerList("获取同步列表", sql,
					new SynchronizeInfoRowWapper(), pager, condition,
					new Object[] {});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}

	/**
	 * 获取同步信息
	 * 
	 * @param id
	 * @return
	 * @throws DaoException
	 */
	public SynchronizeInfo get(String id) throws DaoException {
		String msg = "";
		String sql = "select *   from " + SI_TABLE("turbo_sync_info")
				+ " where id=?";
		try {
			return CM.getDao().get("获取同步信息", sql,
					new SynchronizeInfoRowWapper(), new Object[] { id });
		} catch (DaoException e) {
			msg = "获取同步信息失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	/**
	 * 定时删除同步成功记录
	 * @throws DaoException
	 */
	public void timingDelete(String day) throws DaoException {
		String msg = "";
		String sql = "delete  from " + SI_TABLE("turbo_sync_info")
				+ " where (sysdate - mtime) >? and status=1 ";
		try {
			CM.getDao().delete("定时删除同步成功记录", sql, new Object[] {day});
		} catch (DaoException e) {
			msg = "定时删除同步成功记录";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}

	public List<SynchronizeInfo> getAllNeedReSyncData() throws DaoException {
		WhereCondition condition = new WhereCondition();
		condition.where().eq("STATUS", SUCCESSFAIL.FAIL.getStatus()).orderBy("TONODE");
		try {
			return CM.getDao().listModule("获取所有需要手动发送的数据", SynchronizeInfo.class, condition);
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}

}
